from flask import Blueprint,request

from applications.extensions import db
from applications.models import DictData,DictType


bp = Blueprint('dict', __name__, url_prefix='/dict')


# 用户管理
@bp.get('/<string:power>')
def main(power):
    query = db.session.query( DictData).join(DictType).filter(DictData.enable==1,DictType.enable==1,DictType.type_code==power ).all()
    items=[]
    for item in query:
        items.append(item.to_dict())
    if items:
        return {'success':True,'data':items,'msg':'成功'}
    else:
        return {'success': False, 'data': items, 'msg': '没有单位数据，请前往数据字典添加'}

# 用户管理
@bp.post('/<string:power>')
def get_Unit(power):
    req_json = request.get_json(force=True)
    id = req_json.get("id")
    one=DictData.query.filter_by(id=id).first()
    query = db.session.query( DictData).join(DictType).filter(DictData.enable==1,DictType.enable==1,DictType.type_code==power ).all()
    items=[]
    is_in=False
    for item in query:
        if one==item:
            is_in=True
        items.append(item.to_dict())
    if not is_in:
        items.append(one.to_dict())
    if items:
        return {'success':True,'data':items,'msg':'成功'}
    else:
        return {'success': False, 'data': items, 'msg': '没有单位数据，请前往数据字典添加'}
